package com.sl.au.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventTask;
import com.sl.au.entity.EventType;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;

public interface EventReportRepository extends JpaRepository<EventReport, String> {

	/**
	 * 快速查询
	 * 
	 * @param key
	 * @return
	 */
	@Query("select a from EventReport a where a.reportContent like %?1% or a.reportEventType.eventName like %?1%")
	Page<EventReport> getByKey(String key, Pageable pageable);

	@Query("select a from EventReport a where a.reportEventVariable.title=?1 and (a.reportContent like %?2% or a.reportEventType.eventName like %?2%)")
	Page<EventReport> getByEventVariableAndKey(String eventVariable, String key, Pageable pageable);

	@Query("select a from EventReport a where a.reportEventVariable.title=?1")
	Page<EventReport> getByEventVariable(String eventVariable, Pageable pageable);

	@Query("select a from EventReport a where a.reportEventVariable.id=?2 and (a.reportContent like %?1% or a.reportUser.name like %?1%)")
	Page<EventReport> getFinishedReportPageByKey(String key, String optionId, Pageable pageable);

	@Query("select a from EventReport a where a.reportEventVariable.id=?1")
	Page<EventReport> getFinishedReportPageNoKey(String optionId, Pageable pageable);

	Page<EventReport> findByReportUser(User reportUser, Pageable pageable);

	@Query("select e from EventTask e where e.eventId=?1 order by e.eventTaskTime desc")
	List<EventTask> findProcedures(String reportId);

	@Query("select count(e) from EventReport e where e.reportUser.id=?1 and e.reportEventVariable.id = ?2")
	int getCountByUserAndType(String userId, String type);

	@Query("select count(e) from EventReport e where e.reportUser.id=?1")
	int getCountByUser(String userId);

	@Query("select a from EventReport a where a.reportUser.id = ?1 and a.reportEventVariable.id=?2")
	Page<EventReport> getReportPageByUserAndType(String userId, String optionId, Pageable pageable);

	@Query("select a from EventReport a where a.reportUser.id = ?1 and a.reportEventVariable.id in ?2")
	Page<EventReport> getReportPageByUserAndTypes(String user, String[] types, Pageable pageable);

	@Query("select a from EventReport a where a.id!=?1 and a.latitude=?2 and a.longitude=?3 and a.reportEventType=?4")
	List<EventReport> findInitedInfo(String id, double latitude, double longitude, EventType eventType);

	@Query("select a from EventReport a where a.id!=?1 and ((a.latitude=?2 and a.longitude=?3) or a.reportAdress=?5) and a.reportEventType=?4")
	Page<EventReport> findInitedInfo(Pageable pageable, String id, double latitude, double longitude,
			EventType eventType, String addr);

	@Query("select count(*) from EventReport a where a.id!=?1 and ((a.latitude=?2 and a.longitude=?3) or a.reportAdress=?5) and a.reportEventType=?4")
	int findInitedInfoCount(String id, double latitude, double longitude, EventType eventType, String addr);
	
	@Query("select a from EventReport a where a.reportAccTime >= ?1 and a.reportAccTime <= ?2")
	Page<EventReport> getPageByEventDate(Date begin, Date end, Pageable pageable);

	@Query("select count(a) from EventReport a where a.reportAccTime >= ?1 and a.reportAccTime <= ?2")
	int getPageCountByEventDate(Date begin, Date end);
}
